package com.tiyende.credit.loop

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Typeface
import com.tiyende.credit.loop.base.BaseApplication
import com.tiyende.credit.loop.event.TiyendeEventPublisher
import com.tiyende.credit.loop.request.HttpHelper
import com.tiyende.credit.loop.request.HttpLoader
import com.tiyende.credit.loop.utils.TiyendeDateUtil
import com.tiyende.credit.loop.utils.helper.TiyendeAnalysisHelper
import com.tiyende.credit.loop.utils.log.TiyendeLogger
import com.tiyende.credit.loop.utils.shared.TiyendeSharedHelper
import com.tiyende.credit.loop.utils.shared.TiyendeSharedKey
import com.tiyende.credit.loop.utils.track.TrackPointManager

/**
 * App
 * @Author wwdeng
 * @CreateTime: 2024/11/18 13:35
 * @Annotation：
 */
class TiyendeApp: BaseApplication() {
    //事件订阅
    lateinit var publisher: TiyendeEventPublisher
    private lateinit var typefaceBlack: Typeface
    private lateinit var typefaceBold: Typeface
    private lateinit var typefaceMedium: Typeface
    private lateinit var typefaceRegular: Typeface

    companion object {
        @SuppressLint("StaticFieldLeak")
        lateinit var app: TiyendeApp
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        app = this
        publisher = TiyendeEventPublisher()
    }

    override fun onTerminate() {
        TrackPointManager.getInstance().release()
        super.onTerminate()
    }

    override fun onCreate() {
        super.onCreate()
        typefaceBlack = Typeface.createFromAsset(assets,"fonts/Roboto-Black.ttf")
        typefaceBold = Typeface.createFromAsset(assets,"fonts/Roboto-Bold.ttf")
        typefaceMedium = Typeface.createFromAsset(assets,"fonts/Roboto-Medium.ttf")
        typefaceRegular = Typeface.createFromAsset(assets,"fonts/Roboto-Regular.ttf")

        //保存APP启动时间
        val launchTime = TiyendeDateUtil.getFormatTime(System.currentTimeMillis(),"dd/MM/yyyy HH:mm:ss")
        TiyendeSharedHelper.addTiyendeSharedData(TiyendeSharedKey.TIYENDE_APP_START_TIME,launchTime)

        TiyendeSharedHelper.addTiyendeSharedData(TiyendeSharedKey.TIYENDE_SECRET_KEY,"c0xGLIb88JQP6SPT")
        TiyendeSharedHelper.addTiyendeSharedData(TiyendeSharedKey.TIYENDE_SECRET_IV,"kToCRRB356me95NJ")
        initLibConfigs()
    }

    /**
     * 初始化Lib
     */
    private fun initLibConfigs(){
        TiyendeLogger.debug(BuildConfig.isDebug)
        HttpHelper.init(HttpLoader())
        //初始化 analysis
        TiyendeAnalysisHelper.initAnalysisLibAtAppLaunch()
        TrackPointManager.init(this)
    }

    fun getBlackTypeface(): Typeface{
        return typefaceBlack
    }

    fun getBoldTypeface(): Typeface{
        return typefaceBold
    }

    fun getMediumTypeface(): Typeface{
        return typefaceMedium
    }

    fun getRegularTypeface(): Typeface{
        return typefaceRegular
    }

    /**
     * 获取当前手机屏幕的宽
     */
    fun getPhoneWidth(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.widthPixels
    }

    /**
     * 获取当前手机屏幕的高
     */
    fun getPhoneHeight(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.heightPixels
    }
}